<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/2/26
 * Time: 下午4:59
 */
class Bill_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
        $this->load->model("user_model");
        $this->load->library('email');
    }

    // bill_bn 转 bill_id
    public function bill_bn_to_bill_id($bill_bn)
    {
        $sql = 'select id from app_bill WHERE bill_bn = ' . $bill_bn;
        $bill_id = $this->db->query($sql)->row_array()['id'];
        return $bill_id;
    }

    public function bill_bn_to_bill_status($customer_id)
    {
        $sql = 'select status from app_bill WHERE status=0 AND customer_id = ' . $customer_id;
        $status = $this->db->query($sql)->result_array();
        return $status;
    }


    //首页获取延迟的账单
    public function get_delayed_bill($customer_id)
    {
        $sql = 'select * from app_bill WHERE status=2 and noticed != 1 AND customer_id = ' . $customer_id;
        $bills = $this->db->query($sql)->result_array();
        if (count($bills)) {
            $bill = $bills[0];
            $this->db->where(array("id" => $bill['id']))->update('app_bill', array("noticed" => 1));
            return $bill;
        }
        return false;
    }


    public function add_account_period_repayment_apply($customer_id, $bill_id, $receiver_name, $receiver_email, $send_name, $send_email, $remark = '')
    {
        $insert_arr['customer_id'] = $customer_id;
        $insert_arr['bill_id'] = $bill_id;
        $insert_arr['receiver_name'] = $receiver_name;
        $insert_arr['receiver_email'] = $receiver_email;
        $insert_arr['send_name'] = $send_name;
        $insert_arr['send_email'] = $send_email;
        $insert_arr['remark'] = $remark;
        $insert_arr['created_at'] = date('Y-m-d H:i:s', time());
        $insert_arr['verify_status'] = 0;
        $insert_arr['send_status'] = 0;
        $this->db->insert("app_account_period_repayment_apply", $insert_arr);
        $insert_id = $this->db->insert_id();
        if ($insert_id) {
            return $insert_id;
        }
        return false;
    }

    public function send_email($sender_name, $to_email, $title, $content, $mailtype = 'html')
    {
        $this->load->library('email');
        $config['protocol'] = $this->config->item("protocol"); // 使用smtp协议
        $config['smtp_host'] = $this->config->item("smtp_host"); // smtp主机
        $config['smtp_user'] = $this->config->item("smtp_user"); // smtp用户
        $config['smtp_pass'] = $this->config->item("smtp_pass"); // 密码
        $config['crlf'] = "\r\n"; // 设置换行符
        $config['newline'] = "\r\n"; // 设置换行符
        $config['mailtype'] = $mailtype; // 设置邮件类型
        $this->email->initialize($config); // 写入配置项
        $this->email->from($this->config->item("smtp_user"), $sender_name);
        $this->email->to($to_email);
        $this->email->subject($title);
        $this->email->message($content);

        $re = $this->email->send();
        return $re;
    }

    //获取用户逐月账单信息
    public function query_bill_month_by_month($customer_id, $month, $status)
    {
        $sql = "select * from app_bill join app_bill_month on app_bill.id = app_bill_month.bill_id WHERE customer_id = $customer_id and start_repayment_date is not NULL   AND status in $status ORDER BY app_bill.id ASC limit 1";
        $res = $this->db->query($sql)->row_array();
        return $res;

    }

    public function query_bill_month($customer_id, $type)
    {
        $sql = "select * from app_bill join app_bill_month on app_bill.id = app_bill_month.bill_id WHERE customer_id = $customer_id and type = $type ORDER BY app_bill.id DESC ";
        $res = $this->db->query($sql)->row_array();
        return $res;
    }


    //查询按次结算需还款金额
    public function query_bill_time_money($customer_id)
    {
        $sql = "select sum(money) as money from app_bill  WHERE customer_id = $customer_id and type = 2 and status in (0,2) ";
        $res = $this->db->query($sql)->result_array();
        return $res ? $res[0]['money'] : 0.00;
    }

    //查询按次结算最后还款日
    public function query_bill_time_date($customer_id)
    {
        $sql = "select should_repayment_date from app_bill  WHERE customer_id = $customer_id and type = 2 and status in (0,2) ORDER by should_repayment_date ASC ";
        $res = $this->db->query($sql)->row_array();
        return $res ? $res['should_repayment_date'] : NULL;
    }
}